package com.itmk.web.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itmk.annotation.Auth;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVo;
import com.itmk.web.goods.entity.Goods;
import com.itmk.web.goods.entity.GoodsListParm;
import com.itmk.web.goods.entity.MyGoodsParm;
import com.itmk.web.goods.entity.WxIndexParm;
import com.itmk.web.goods.service.GoodsService;
import com.itmk.web.goods_category.service.GoodsCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 *  商品管理
 */
@Slf4j
@RestController
@RequestMapping("/api/goods")
public class GoodsController {

    @Autowired
    GoodsCategoryService goodsCategoryService;
    @Autowired
    private GoodsService goodsService;

    //发布商品
    @CrossOrigin
    @PostMapping("/release")
    public ResultVo release(@RequestBody Goods goods){
        //设置时间
        goods.setCreateTime(new Date());
        goods.setGoodsId(goodsService.maxId());
        goodsService.save(goods);
        return ResultUtils.success("发布成功");
    }


    @Auth
    @CrossOrigin
    @GetMapping("/list")
    public ResultVo getlist(GoodsListParm parm) throws IOException {

        Page<Goods> page = new Page<>(parm.getCurrentPage(), parm.getPageSize());
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(parm.getGoodsName()),Goods::getGoodsName,parm.getGoodsName())
                .orderByDesc(Goods::getCreateTime) .eq(Goods::getDeleteStatus,"0");
        Page<Goods> goodsPage = goodsService.page(page, queryWrapper);
        List<Goods> records = goodsPage.getRecords();
        for (Goods record : records) {
            log.info(record.getImage());
        }

        return ResultUtils.success("查询成功",goodsPage);
    }

    /**
     * 商品上下架
     * @return  2023 12 4
     *
     */
    @Auth
    @CrossOrigin  //跨域请求
    @PostMapping("/upanddown")
    public ResultVo upanddown(@RequestBody Goods parm){

        Goods goods = goodsService.getById(parm.getGoodsId());
        goods.setStatus(parm.getStatus());
        goods.setCategoryName(goodsCategoryService.getById(goods.getCategoryId()).getCategoryName());
        if (goodsService.updateById(goods)){
            //下架商品需要将下架的商品信息在ES中更新
            return ResultUtils.success("设置成功");
        }
        return ResultUtils.error("设置失败");
    }

    /**
     * 用户上下架
     * @param parm
     * @return
     */
    @PostMapping("/upanddownN")
    public ResultVo upanddownN(@RequestBody Goods parm){
        Goods goods = goodsService.getById(parm.getGoodsId());
        goods.setStatus(parm.getStatus());
        if (goodsService.updateById(goods)){
            goods.setCategoryName(goodsCategoryService.getById(parm.getGoodsId()).getCategoryName());
            return ResultUtils.success("设置成功");
        }
        return ResultUtils.error("设置失败");
    }
    /**
     * 推荐首页
     * @return  2023 12 4
     */
    @Auth
    @CrossOrigin
    @PostMapping("/setIndex")
    public ResultVo setIndex(@RequestBody Goods parm){
        Goods goods = goodsService.getById(parm.getGoodsId());
        goods.setSetIndex(parm.getSetIndex());
        if (goodsService.updateById(goods)){
            goods.setCategoryName(goodsCategoryService.getById(goods.getGoodsId()).getCategoryName());
            return ResultUtils.success("设置成功");
        }
        return ResultUtils.error("设置失败");
    }

    /**
     * 商品删除
     * @return  2023 12 4
     */

    @CrossOrigin
    @PostMapping("/delete")
    public ResultVo delete(@RequestBody Goods parm){
        Goods goods = goodsService.getById(parm.getGoodsId());
        goods.setDeleteStatus("1");
        goods.setCategoryName(goodsCategoryService.getById(goods.getGoodsId()).getCategoryName());
        if (goodsService.updateById(goods)){
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.error("删除失败");
    }

    //小程序分页查询
    //小程序首页推荐列表
    @GetMapping("/getIndexList")
    public ResultVo getIndexList(WxIndexParm parm){
        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.lambda().like(StringUtils.isNotEmpty(parm.getKeywords()),Goods::getGoodsName,parm.getKeywords())
                .eq(Goods::getSetIndex,"1")
                .eq(Goods::getStatus,"0")
                .orderByDesc(Goods::getCreateTime)
                .eq(Goods::getDeleteStatus,"0");
        IPage<Goods> page = new Page<>(parm.getCurrentPage(),parm.getPageSize());
        IPage<Goods> list = goodsService.page(page,query);
        return ResultUtils.success("查询成功",list);
    }
  //求购分页查询
    @CrossOrigin
    @GetMapping("/getBuyList")
    public ResultVo getBuyList(GoodsListParm parm){
        Page<Goods> page = new Page<>(parm.getCurrentPage(),parm.getPageSize());
         LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.eq(Goods::getType,"1")
                 .eq(Goods::getDeleteStatus,"0")
                 .like(StringUtils.isNotEmpty(parm.getKeywords()),Goods::getGoodsName,parm.getKeywords())
                 .eq(Goods::getStatus,"0")
                 .eq(StringUtils.isNotEmpty(parm.getCategoryId()),Goods::getCategoryId,parm.getCategoryId());
         goodsService.page(page,queryWrapper);
         return ResultUtils.success("yes",page);
    }
    //小程序闲置分类查询
    @CrossOrigin
    @GetMapping("/getUsedList")
    public ResultVo getUsedList(GoodsListParm parm) {
        Page<Goods> page = new Page<>(parm.getCurrentPage(), parm.getPageSize());
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Goods::getType, "0")
                .eq(Goods::getSellStatus,"0")
                .like(StringUtils.isNotEmpty(parm.getKeywords()), Goods::getGoodsName, parm.getKeywords())
                .eq(StringUtils.isNotEmpty(parm.getCategoryId()), Goods::getCategoryId, parm.getCategoryId())
                .eq(Goods::getDeleteStatus,"0")
                .eq(Goods::getStatus,"0");
        goodsService.page(page, queryWrapper);
        return ResultUtils.success("yes", page);
    }

    //小程序我发布的闲置/求购 分页查询
    @GetMapping("/getMyUnusedList")
    public ResultVo getMyUnusedList(MyGoodsParm parm){
//构造查询条件
        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.lambda().eq(Goods::getUserId,parm.getUserId())
                .eq(Goods::getType,parm.getType())
                .eq(Goods::getDeleteStatus,"0");
//构造分页对象
        IPage<Goods> page = new Page<>
                (parm.getCurrentPage(),parm.getPageSize());
        IPage<Goods> list = goodsService.page(page, query);
        return ResultUtils.success("查询成",list);
    }

    //小程序用户编辑商品
    @PostMapping("/edit")
    public ResultVo edit(@RequestBody Goods goods){
        if(goodsService.updateById(goods)){
            return ResultUtils.success("编辑成功！");
        }
        return ResultUtils.error("编辑失败!");
    }
}
